package persistency.controller;

import java.util.ArrayList;
import java.util.Collection;

import model.Business;
import model.BusinessTypeEnum;
import model.RetrainingStudent;
import model.Student;
import model.Teacher;
import persistency.ArgIO;

public class StudentController extends BusinessController{

	private StringBuilder callableStatement;

	
	private static StudentController instance = new StudentController();
	private StudentController(){}
	
	public static StudentController instance(){
		return instance;
	}
	
	public boolean insertStudent(Student student) {
		callableStatement = new StringBuilder("{call sp_insertstudent(?,?)}");
		return insert(student,callableStatement,2);
	}
	
	public Student getStudent(String id){
		return (Student)getBusinessObject(id,new StringBuilder("{call sp_getbyidstudent(?)}"),BusinessTypeEnum.STUDENT);
	}
	
	public Collection<Student> getAll() {
		Collection<Student> col = new ArrayList<Student>();
		col.addAll((Collection<? extends Student>) getBusinessObjects(new StringBuilder("{call sp_getallstudent}"),BusinessTypeEnum.STUDENT));
		return col;
	}

	@Override
	public void prepare(Business business, Object[] args, int[] argsType, ArgIO[] argsIO, int i) throws Exception {
		/*
	 in studentnumber varchar(25),
    in name varchar(25)
		 */
		
		Student student = (Student) business;
		switch (i) {
		case 0: {
			args[i] = student.getStudentNumber();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 1: {
			args[i] = student.getName();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		
		default:
			throw new Exception("Error while creating SP createCourse");
		}
		
	}
	
}
